package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.SystemDictionary;
import com.xmg.p2p.base.domain.SystemDictionaryItem;
import com.xmg.p2p.base.mapper.SystemDictionaryItemMapper;
import com.xmg.p2p.base.mapper.SystemDictionaryMapper;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.SysDictQO;
import com.xmg.p2p.base.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDictServiceImpl implements ISysDictService{
    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;
    @Autowired
    private SystemDictionaryItemMapper itemMapper;
    @Override
    public PageResult query(SysDictQO qo) {
        int count = systemDictionaryMapper.queryCount(qo);
        if(count>0){
            return new PageResult(systemDictionaryMapper.query(qo),count,qo.getCurrentPage(),qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    @Override
    public void saveOrUpdateDict(SystemDictionary dict) {
        if(dict.getId()==null){
            systemDictionaryMapper.insert(dict);
        }else {
            systemDictionaryMapper.updateByPrimaryKey(dict);
        }
    }

    @Override
    public List<SystemDictionary> selectAllDict() {
        return systemDictionaryMapper.selectAll();
    }

    @Override
    public PageResult queryItem(SysDictQO qo) {
        int count = itemMapper.queryCount(qo);
        if(count>0){
            return new PageResult(itemMapper.query(qo),count,qo.getCurrentPage(),qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    @Override
    public void saveOrUpdateItem(SystemDictionaryItem item) {
        if(item.getId()==null){
            itemMapper.insert(item);
        }else {
            itemMapper.updateByPrimaryKey(item);
        }
    }

    @Override
    public List<SystemDictionaryItem> selectByParentSn(String parentSn) {
        return itemMapper.selectByParentSn(parentSn);
    }
}
